<template>
  <!-- eslint-disable vue/html-self-closing -->
  <div>
    <el-form :inline="true" :model="cForm" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select v-model="cForm.category1Id" :disabled="show" placeholder="请选择" @change="handler1">
          <el-option v-for="c1 in list1" :key="c1.id" :label="c1.name" :value="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="cForm.category2Id" :disabled="show" placeholder="请选择" @change="handler2">
          <el-option v-for="c2 in list2" :key="c2.id" :label="c2.name" :value="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="cForm.category3Id" :disabled="show" placeholder="请选择" @change="handler3">
          <el-option v-for="c3 in list3" :key="c3.id" :label="c3.name" :value="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
/* eslint-disable spaced-comment */
/* eslint-disable eqeqeq */
/* eslint-disable comma-dangle */
/* eslint-disable quotes */
/* eslint-disable semi */
export default {
  name: 'CategorySelect',
  props: ['show'],
  data() {
    return {
      //一级分类
      list1: [],
      //二级分类
      list2: [],
      //三级分类
      list3: [],
      //收集相应的1 2 3级分类的id
      cForm: {
        category1Id: '',
        category2Id: '',
        category3Id: ''
      }
    }
  },
  //组件挂载完毕获取一级分类
  mounted() {
    //获取一级分类的方法
    this.getCategory1List()
  },
  methods: {
    //获取一级分类数据
    async getCategory1List() {
      const result = await this.$API.attr.reqCategory1List()
      if (result.code == 200) {
        this.list1 = result.data
      }
    },
    //当一级分类的option发生变化时获取相应二级分类的数据
    async handler1() {
      //清除数据
      this.list2 = []
      this.list3 = []
      this.cForm.category2Id = ''
      this.cForm.category3Id = ''
      //获取数据
      const result = await this.$API.attr.reqCategory2List(
        this.cForm.category1Id
      )
      if (result.code == 200) {
        this.list2 = result.data
      }
      //将category1Id传给父组件使用
      this.$emit('getCategoryId', {
        categoryId: this.cForm.category1Id,
        level: 1
      })
    },
    //当二级分类的option发生变化时获取相应三级分类的数据
    async handler2() {
      //清除数据
      this.list3 = []
      this.cForm.category3Id = ''
      //获取数据
      const result = await this.$API.attr.reqCategory3List(
        this.cForm.category2Id
      )
      if (result.code == 200) {
        this.list3 = result.data
      }
      //将category2Id传给父组件使用
      this.$emit('getCategoryId', {
        categoryId: this.cForm.category2Id,
        level: 2
      })
    },
    //三级分类的事件回调，暂时没用
    handler3() {
      //将category3Id传给父组件使用
      this.$emit('getCategoryId', {
        categoryId: this.cForm.category3Id,
        level: 3
      })
    }
  }
}
</script>

<style>
</style>
